using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;

namespace GameLib.GameScreen
{
    public class ScreenManager : DrawableGameComponent
    {
        //public static Texture2D dummyTexture;
        
        Texture2D blankTexture;

        SpriteBatch spriteBatch;
        List<GameScreen> screens = new List<GameScreen>();
        int ScreenWaitToRemove = -1;
        ScreenName ScreenWaitToAdd;
        bool isPause = false;

        public GraphicsDevice Graphics
        {
            get { return Game.GraphicsDevice; }
        }

        public ScreenManager(Game game)
            : base(game)
        {
            
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            //dummyTexture = new Texture2D(GraphicsDevice, 1, 1);
            //dummyTexture.SetData(new Color[] { Color.White });
            blankTexture = Game.Content.Load<Texture2D>(@"Images\Blank");
            SoundManager.Init(Game.Content);
            base.LoadContent();
        }

        public void FirstScr()
        {
            GameTitleScreen titleScr = new GameTitleScreen(this);
            screens.Add(titleScr);
        }

        public void Pause()
        {
            isPause = true;
        }

        public void Resume()
        {
            isPause = false;
        }

        public void RemoveAndAdd(GameScreen removeScreen, ScreenName addScreen)
        {
            if (ScreenWaitToRemove == -1)
            {
                removeScreen.FadeOut();
                ScreenWaitToAdd = addScreen;
                ScreenWaitToRemove = screens.IndexOf(removeScreen);
            }
        }

        public void RemoveScreen(GameScreen screen)
        {
            screens.Remove(screen);
            screen.UnLoad();
        }

        public void AddScreen(GameScreen screen)
        {
            screens.Add(screen);
        }

        public void FadeBackBufferToBlack(float alpha)
        {
            spriteBatch.Draw(blankTexture, GraphicsDevice.Viewport.Bounds, Color.Black * alpha);
        }

        public override void Update(GameTime gameTime)
        {
            if (ScreenWaitToRemove != -1)
            {
                if (screens[ScreenWaitToRemove].TransComplete)
                {
                    screens.RemoveAt(ScreenWaitToRemove);
                    ScreenWaitToRemove = -1;
                    if (ScreenWaitToAdd == ScreenName.GamePlay)
                    {
                        LoadingScreen.LoadScreen(this, ScreenWaitToAdd);
                    }
                    else
                    {
                        LoadingScreen.LoadScreen(this, ScreenWaitToAdd);
                    }
                }
            }

            if (!isPause)
            {
                for (int i = 0; i < screens.Count; i++)
                {
                    screens[i].Update(gameTime);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            foreach (GameScreen screen in screens)
            {
                screen.Draw(spriteBatch);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void Exit()
        {
            Game.Exit();
        }
    }
}
